1
|
|
|
/*jslint |
2
|
|
|
regexp: true |
3
|
|
|
indent: 4 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
/*global |
7
|
|
|
GeographicLib, google |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
var Coordinates = {}; |
11
|
|
|
Coordinates.m_format = "DM"; |
12
|
|
|
Coordinates.m_geod = GeographicLib.Geodesic.WGS84; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
Coordinates.setFormat = function (format) { |
16
|
|
|
'use strict'; |
17
|
|
|
|
18
|
|
|
if (format === "DM" || format === "DMS" || format === "D") { |
19
|
|
|
this.m_format = format; |
20
|
|
|
} |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
Coordinates.validLat = function (lat) { |
25
|
|
|
'use strict'; |
26
|
|
|
|
27
|
|
|
return lat !== null && lat !== undefined && !isNaN(lat) && -90.0 <= lat && lat <= 90.0; |
28
|
|
|
}; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
Coordinates.validLng = function (lng) { |
32
|
|
|
'use strict'; |
33
|
|
|
|
34
|
|
|
return lng !== null && lng !== undefined && !isNaN(lng) && -180.0 <= lng && lng <= 180.0; |
35
|
|
|
}; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
Coordinates.valid = function (lat, lng) { |
39
|
|
|
'use strict'; |
40
|
|
|
|
41
|
|
|
return this.validLat(lat) && this.validLng(lng); |
42
|
|
|
}; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
Coordinates.toLatLng = function (lat, lng) { |
46
|
|
|
'use strict'; |
47
|
|
|
|
48
|
|
|
if (this.valid(lat, lng)) { |
49
|
|
|
return new google.maps.LatLng(lat, lng); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return null; |
53
|
|
|
}; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
Coordinates.fromString = function (coordsString) { |
57
|
|
|
'use strict'; |
58
|
|
|
|
59
|
|
|
var coords = null; |
|
|
|
|
60
|
|
|
|
61
|
|
|
coords = this.fromStringDM(coordsString); |
62
|
|
|
if (coords) { |
63
|
|
|
return coords; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
coords = this.fromStringDMS(coordsString); |
67
|
|
|
if (coords) { |
68
|
|
|
return coords; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
coords = this.fromStringD(coordsString); |
72
|
|
|
if (coords) { |
73
|
|
|
return coords; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return null; |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
Coordinates._sanitize = function (s) { |
81
|
|
|
'use strict'; |
82
|
|
|
|
83
|
|
|
var sanitized = "", |
84
|
|
|
commas = 0, |
85
|
|
|
periods = 0, |
86
|
|
|
i; |
87
|
|
|
|
88
|
|
|
for (i = 0; i < s.length; i++) { |
89
|
|
|
if ((s[i] == 'o') || (s[i] == 'O')) { |
90
|
|
|
// map 'O'/'o' to 'E' (German 'Ost' = 'East') |
91
|
|
|
sanitized += 'E'; |
92
|
|
|
} else if (s[i].match(/[a-z0-9\-]/i)) { |
93
|
|
|
sanitized += s[i].toUpperCase(); |
94
|
|
|
} else if (s[i] == '.') { |
95
|
|
|
periods += 1; |
96
|
|
|
sanitized += s[i]; |
97
|
|
|
} else if (s[i] == ',') { |
98
|
|
|
commas += 1; |
99
|
|
|
sanitized += s[i]; |
100
|
|
|
} else { |
101
|
|
|
sanitized += ' '; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// try to map commas to spaces or periods |
106
|
|
|
if ((commas == 1) && ((periods == 0) || (periods >= 2))) { |
107
|
|
|
return sanitized.replace(/,/g, ' ') |
108
|
|
|
} else if ((commas >= 1) && (periods == 0)) { |
109
|
|
|
return sanitized.replace(/,/g, '.') |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return sanitized; |
113
|
|
|
}; |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
Coordinates._create = function (h1, d1, m1, s1, h2, d2, m2, s2) { |
117
|
|
|
'use strict'; |
118
|
|
|
|
119
|
|
|
var c1, c2, lat, lng; |
120
|
|
|
|
121
|
|
|
if (h1 && d1 < 0) { |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
if (m1 < 0 || m1 >= 60) { |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
if (s1 < 0 || s1 >= 60) { |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (h2 && d2 < 0) { |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
if (m2 < 0 || m2 >= 60) { |
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
if (s2 < 0 || s2 >= 60) { |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
c1 = d1 + (m1 / 60.0) + (s1 / 3600.0); |
142
|
|
|
c2 = d2 + (m2 / 60.0) + (s2 / 3600.0); |
143
|
|
|
|
144
|
|
|
if (!h1 && !h2) { |
145
|
|
|
lat = c1; |
146
|
|
|
lng = c2; |
147
|
|
|
} else if ((h1 == 'N' || h1 == 'S') && (h2 == 'E' || h2 == 'W')) { |
148
|
|
|
lat = c1; |
149
|
|
|
lng = c2; |
150
|
|
|
if (h1 == 'S') { |
151
|
|
|
lat = -lat; |
152
|
|
|
} |
153
|
|
|
if (h2 == 'W') { |
154
|
|
|
lng = -lng; |
155
|
|
|
} |
156
|
|
|
} else if ((h2 == 'N' || h2 == 'S') && (h1 == 'E' || h1 == 'W')) { |
157
|
|
|
lat = c2; |
158
|
|
|
lng = c1; |
159
|
|
|
if (h2 == 'S') { |
160
|
|
|
lat = -lat; |
161
|
|
|
} |
162
|
|
|
if (h1 == 'W') { |
163
|
|
|
lng = -lng; |
164
|
|
|
} |
165
|
|
|
} else { |
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (lat < -90 || lat > 90 || lng < -180 || lng > 180) { |
170
|
|
|
return null |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return Coordinates.toLatLng(lat, lng); |
174
|
|
|
}; |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
Coordinates.fromStringDMS = function (coordsString) { |
178
|
|
|
'use strict'; |
179
|
|
|
|
180
|
|
|
var s = this._sanitize(coordsString), |
181
|
|
|
pattern, |
182
|
|
|
m, |
183
|
|
|
coords; |
184
|
|
|
|
185
|
|
|
// H D M S.S |
186
|
|
|
pattern = /^\s*([NEWS])\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*$/; |
187
|
|
|
if (m = s.match(pattern)) { |
188
|
|
|
coords = Coordinates._create(m[1], parseFloat(m[2]), parseFloat(m[3]), parseFloat(m[4]), |
189
|
|
|
m[5], parseFloat(m[6]), parseFloat(m[7]), parseFloat(m[8])); |
190
|
|
|
if (coords) { |
191
|
|
|
return coords; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// D H M S.S |
196
|
|
|
pattern = /^\s*(\d+)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s+(\d+)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*$/; |
197
|
|
|
if (m = s.match(pattern)) { |
198
|
|
|
coords = Coordinates._create(m[2], parseFloat(m[1]), parseFloat(m[3]), parseFloat(m[4]), |
199
|
|
|
m[6], parseFloat(m[5]), parseFloat(m[7]), parseFloat(m[8])); |
200
|
|
|
if (coords) { |
201
|
|
|
return coords; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// D M S.S H |
206
|
|
|
pattern = /^\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*$/; |
207
|
|
|
if (m = s.match(pattern)) { |
208
|
|
|
coords = Coordinates._create(m[4], parseFloat(m[1]), parseFloat(m[2]), parseFloat(m[3]), |
209
|
|
|
m[8], parseFloat(m[5]), parseFloat(m[6]), parseFloat(m[7])); |
210
|
|
|
if (coords) { |
211
|
|
|
return coords; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// D M S.S |
216
|
|
|
pattern = /^\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s+(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*$/; |
217
|
|
|
if (m = s.match(pattern)) { |
218
|
|
|
coords = Coordinates._create('N', parseFloat(m[1]), parseFloat(m[2]), parseFloat(m[3]), |
219
|
|
|
'E', parseFloat(m[4]), parseFloat(m[5]), parseFloat(m[6])); |
220
|
|
|
if (coords) { |
221
|
|
|
return coords; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return null; |
226
|
|
|
}; |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
Coordinates.fromStringDM = function (coordsString) { |
230
|
|
|
'use strict'; |
231
|
|
|
|
232
|
|
|
var s = this._sanitize(coordsString), |
233
|
|
|
pattern, |
234
|
|
|
m, |
235
|
|
|
coords; |
236
|
|
|
|
237
|
|
|
// H D M.M |
238
|
|
|
pattern = /^\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*$/; |
239
|
|
|
if (m = s.match(pattern)) { |
240
|
|
|
coords = Coordinates._create(m[1], parseFloat(m[2]), parseFloat(m[3]), 0, |
241
|
|
|
m[4], parseFloat(m[5]), parseFloat(m[6]), 0); |
242
|
|
|
if (coords) { |
243
|
|
|
return coords; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// D H M.M |
248
|
|
|
pattern = /^\s*(\d+)\s*([NEWS])\s*(\d+\.?\d*)\s+(\d+)\s*([NEWS])\s*(\d+\.?\d*)\s*$/; |
249
|
|
|
if (m = s.match(pattern)) { |
250
|
|
|
coords = Coordinates._create(m[2], parseFloat(m[1]), parseFloat(m[3]), 0, |
251
|
|
|
m[5], parseFloat(m[4]), parseFloat(m[6]), 0); |
252
|
|
|
if (coords) { |
253
|
|
|
return coords; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// D M.M H |
258
|
|
|
pattern = /^\s*(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*$/; |
259
|
|
|
if (m = s.match(pattern)) { |
260
|
|
|
coords = Coordinates._create(m[3], parseFloat(m[1]), parseFloat(m[2]), 0, |
261
|
|
|
m[6], parseFloat(m[4]), parseFloat(m[5]), 0); |
262
|
|
|
if (coords) { |
263
|
|
|
return coords; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// D M.M |
268
|
|
|
pattern = /^\s*(\d+)\s+(\d+\.?\d*)\s+(\d+)\s+(\d+\.?\d*)\s*$/; |
269
|
|
|
if (m = s.match(pattern)) { |
270
|
|
|
coords = Coordinates._create('N', parseFloat(m[1]), parseFloat(m[2]), 0, |
271
|
|
|
'E', parseFloat(m[3]), parseFloat(m[4]), 0); |
272
|
|
|
if (coords) { |
273
|
|
|
return coords; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return null; |
278
|
|
|
}; |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
Coordinates.fromStringD = function (coordsString) { |
282
|
|
|
'use strict'; |
283
|
|
|
|
284
|
|
|
var s = this._sanitize(coordsString), |
285
|
|
|
pattern, |
286
|
|
|
m, |
287
|
|
|
coords; |
288
|
|
|
|
289
|
|
|
// H D.D |
290
|
|
|
pattern = /^\s*([NEWS])\s*(\d+\.?\d*)\s*([NEWS])\s*(\d+\.?\d*)\s*$/; |
291
|
|
|
if (m = s.match(pattern)) { |
292
|
|
|
coords = Coordinates._create(m[1], parseFloat(m[2]), 0, 0, |
293
|
|
|
m[3], parseFloat(m[4]), 0, 0); |
294
|
|
|
if (coords) { |
295
|
|
|
return coords; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
// D.D H |
300
|
|
|
pattern = /^\s*(\d+\.?\d*)\s*([NEWS])\s*(\d+\.?\d*)\s*([NEWS])\s*$/; |
301
|
|
|
if (m = s.match(pattern)) { |
302
|
|
|
coords = Coordinates._create(m[2], parseFloat(m[1]), 0, 0, |
303
|
|
|
m[4], parseFloat(m[3]), 0, 0); |
304
|
|
|
if (coords) { |
305
|
|
|
return coords; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// D.D |
310
|
|
|
pattern = /^\s*(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s*$/; |
311
|
|
|
if (m = s.match(pattern)) { |
312
|
|
|
coords = Coordinates._create(null, parseFloat(m[1]), 0, 0, |
313
|
|
|
null, parseFloat(m[2]), 0, 0); |
314
|
|
|
if (coords) { |
315
|
|
|
return coords; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return null; |
320
|
|
|
}; |
321
|
|
|
|
322
|
|
|
|
323
|
|
|
Coordinates.toStringDM = function (coords) { |
324
|
|
|
'use strict'; |
325
|
|
|
|
326
|
|
|
var lat = coords.lat(), |
327
|
|
|
lat_h = "N", |
328
|
|
|
lat_deg, |
329
|
|
|
lat_min, |
330
|
|
|
lat_mmin, |
331
|
|
|
lat_rest, |
332
|
|
|
lng = coords.lng(), |
333
|
|
|
lng_h = "E", |
334
|
|
|
lng_deg, |
335
|
|
|
lng_min, |
336
|
|
|
lng_mmin, |
337
|
|
|
lng_rest, |
338
|
|
|
s; |
339
|
|
|
|
340
|
|
|
if (lat < 0) { |
341
|
|
|
lat_h = "S"; |
342
|
|
|
lat = -lat; |
343
|
|
|
} |
344
|
|
|
lat_deg = Math.floor(lat); |
345
|
|
|
lat_rest = lat - lat_deg; |
346
|
|
|
lat_min = Math.floor(lat_rest * 60); |
347
|
|
|
lat_rest = lat_rest * 60 - lat_min; |
348
|
|
|
lat_mmin = Math.floor(Math.round(lat_rest * 1000)); |
349
|
|
|
while (lat_mmin >= 1000) { |
350
|
|
|
lat_mmin -= 1000; |
351
|
|
|
lat_min += 1; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
if (lng < 0) { |
355
|
|
|
lng_h = "W"; |
356
|
|
|
lng = -lng; |
357
|
|
|
} |
358
|
|
|
lng_deg = Math.floor(lng); |
359
|
|
|
lng_rest = lng - lng_deg; |
360
|
|
|
lng_min = Math.floor(lng_rest * 60); |
361
|
|
|
lng_rest = lng_rest * 60 - lng_min; |
362
|
|
|
lng_mmin = Math.floor(Math.round(lng_rest * 1000)); |
363
|
|
|
while (lng_mmin >= 1000) { |
364
|
|
|
lng_mmin -= 1000; |
365
|
|
|
lng_min += 1; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
s = lat_h + |
369
|
|
|
" " + |
370
|
|
|
this.zeropad(lat_deg, 2) + |
371
|
|
|
" " + |
372
|
|
|
this.zeropad(lat_min, 2) + |
373
|
|
|
"." + |
374
|
|
|
this.zeropad(lat_mmin, 3) + |
375
|
|
|
" " + |
376
|
|
|
lng_h + |
377
|
|
|
" " + |
378
|
|
|
this.zeropad(lng_deg, 3) + |
379
|
|
|
" " + |
380
|
|
|
this.zeropad(lng_min, 2) + |
381
|
|
|
"." + |
382
|
|
|
this.zeropad(lng_mmin, 3); |
383
|
|
|
return s; |
384
|
|
|
}; |
385
|
|
|
|
386
|
|
|
|
387
|
|
|
Coordinates.toStringDMS = function (coords) { |
388
|
|
|
'use strict'; |
389
|
|
|
|
390
|
|
|
var lat = coords.lat(), |
391
|
|
|
lat_h = "N", |
392
|
|
|
lat_deg, |
393
|
|
|
lat_min, |
394
|
|
|
lat_sec, |
395
|
|
|
lat_rest, |
396
|
|
|
lng = coords.lng(), |
397
|
|
|
lng_h = "E", |
398
|
|
|
lng_deg, |
399
|
|
|
lng_min, |
400
|
|
|
lng_sec, |
401
|
|
|
lng_rest, |
402
|
|
|
s; |
403
|
|
|
|
404
|
|
|
if (lat < 0) { |
405
|
|
|
lat_h = "S"; |
406
|
|
|
lat = -lat; |
407
|
|
|
} |
408
|
|
|
lat_deg = Math.floor(lat); |
409
|
|
|
lat_rest = lat - lat_deg; |
410
|
|
|
lat_min = Math.floor(lat_rest * 60); |
411
|
|
|
lat_rest = lat_rest * 60 - lat_min; |
412
|
|
|
lat_sec = lat_rest * 60.0; |
413
|
|
|
|
414
|
|
|
if (lng < 0) { |
415
|
|
|
lng_h = "W"; |
416
|
|
|
lng = -lng; |
417
|
|
|
} |
418
|
|
|
lng_deg = Math.floor(lng); |
419
|
|
|
lng_rest = lng - lng_deg; |
420
|
|
|
lng_min = Math.floor(lng_rest * 60); |
421
|
|
|
lng_rest = lng_rest * 60 - lng_min; |
422
|
|
|
lng_sec = lng_rest * 60.0; |
423
|
|
|
|
424
|
|
|
s = lat_h + |
425
|
|
|
" " + |
426
|
|
|
this.zeropad(lat_deg, 2) + |
427
|
|
|
" " + |
428
|
|
|
this.zeropad(lat_min, 2) + |
429
|
|
|
" " + |
430
|
|
|
this.zeropad(lat_sec.toFixed(2), 5) + |
431
|
|
|
" " + |
432
|
|
|
lng_h + |
433
|
|
|
" " + |
434
|
|
|
this.zeropad(lng_deg, 3) + |
435
|
|
|
" " + |
436
|
|
|
this.zeropad(lng_min, 2) + |
437
|
|
|
" " + |
438
|
|
|
this.zeropad(lng_sec.toFixed(2), 5); |
439
|
|
|
|
440
|
|
|
return s; |
441
|
|
|
}; |
442
|
|
|
|
443
|
|
|
|
444
|
|
|
Coordinates.toStringD = function (coords) { |
445
|
|
|
'use strict'; |
446
|
|
|
|
447
|
|
|
var lat = coords.lat(), |
448
|
|
|
lat_h = "N", |
449
|
|
|
lng = coords.lng(), |
450
|
|
|
lng_h = "E"; |
451
|
|
|
|
452
|
|
|
if (lat < 0) { |
453
|
|
|
lat_h = "S"; |
454
|
|
|
lat = -lat; |
455
|
|
|
} |
456
|
|
|
if (lng < 0) { |
457
|
|
|
lng_h = "W"; |
458
|
|
|
lng = -lng; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return lat_h + " " + lat.toFixed(6) + " " + lng_h + " " + lng.toFixed(6); |
462
|
|
|
}; |
463
|
|
|
|
464
|
|
|
|
465
|
|
|
Coordinates.toString = function (coords) { |
466
|
|
|
'use strict'; |
467
|
|
|
|
468
|
|
|
if (this.m_format === "DM") { |
469
|
|
|
return this.toStringDM(coords); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
if (this.m_format === "DMS") { |
473
|
|
|
return this.toStringDMS(coords); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
if (this.m_format === "D") { |
477
|
|
|
return this.toStringD(coords); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
return this.toStringDM(coords); |
481
|
|
|
}; |
482
|
|
|
|
483
|
|
|
|
484
|
|
|
Coordinates.dist_angle_geodesic = function (startpos, endpos) { |
485
|
|
|
'use strict'; |
486
|
|
|
|
487
|
|
|
var t = this.m_geod.Inverse(startpos.lat(), startpos.lng(), endpos.lat(), endpos.lng()), |
488
|
|
|
a = t.azi1; |
489
|
|
|
if (a < 0) { |
490
|
|
|
a += 360.0; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
return { dist: t.s12, angle: a }; |
494
|
|
|
}; |
495
|
|
|
|
496
|
|
|
|
497
|
|
|
Coordinates.projection_geodesic = function (startpos, angle, distance) { |
498
|
|
|
'use strict'; |
499
|
|
|
|
500
|
|
|
var t = this.m_geod.Direct(startpos.lat(), startpos.lng(), angle, distance); |
501
|
|
|
return new google.maps.LatLng(t.lat2, t.lon2); |
502
|
|
|
}; |
503
|
|
|
|
504
|
|
|
|
505
|
|
|
Coordinates.zeropad = function (num, width) { |
506
|
|
|
'use strict'; |
507
|
|
|
|
508
|
|
|
var s = String(num); |
509
|
|
|
while (s.length < width) { |
510
|
|
|
s = "0" + s; |
511
|
|
|
} |
512
|
|
|
return s; |
513
|
|
|
}; |
514
|
|
|
|